import Vue from 'vue'
import VueRouter from 'vue-router'

// 路由导入
import layout from '@/views/layout.vue'
import login from '@/views/user/login.vue'
import home from '@/views/home/home.vue'
import movie from '@/views/movie/movie.vue'
import qa from '@/views/qa/qa.vue'
import user from '@/views/user/user.vue'
import notfound from '@/views/notfound/notfound.vue'
import edit from '@/views/user/edit.vue'
import article from '@/views/home/article.vue'
import search from '@/views/home/search.vue'
import searchResult from '@/views/home/searchResult.vue'
import student from '@/views/user/student.vue'

Vue.use(VueRouter)

const routes = [{
    path: '/login',
    name: 'login',
    component: login
  },
  {
    path: '/edit',
    name: 'edit',
    component: edit
  },
  {
    path: '/article',
    name: 'article',
    component: article
  },
  {
    path: '/search',
    name: 'search',
    component: search
  },
  {
    path: '/searchResult',
    name: 'searchResult',
    component: searchResult
  },
  {
    path: '/student',
    name: 'student',
    component: student
  },
  {
    path: '/',
    name: 'layout',
    component: layout,

    children: [{
        path: '/home',
        name: 'home',
        component: home
      }, {
        path: '/movie',
        name: 'movie',
        component: movie
      }, {
        path: '/qa',
        name: 'qa',
        component: qa
      }, {
        path: '/user',
        name: 'user',
        component: user,
        meta: {
          isLogin: true
        }
      },
      {
        path: '/',
        redirect: '/home'
      }
    ]
  },
  {
    path: '/notfound',
    name: 'notfound',
    component: notfound
  },
  {
    path: '*',
    redirect: '/notfound'
  }
]

const router = new VueRouter({
  routes
})
import {
  getToken
} from '@/utils/token.js'
import {
  Toast
} from 'vant'


router.beforeEach((to, from, next) => {
  // ${//to and from are Route Object,next() must be called to resolve the hook}
  if (to.meta.isLogin) {
    if (getToken()) {
      next()
    } else {
      Toast.fail('请先登录')
      next('./login')
    }
  } else {
    next()
  }
})

export default router
